{% extends 'admin.phtml' %}
{% block title %}Вход{% endblock %}
{% block head %}
<script type="text/javascript">
    $(function () {
        $("#username").focus();
    });
</script>
{% endblock  %}

{% block body %}
<div class="row">
    <div class="span6">
        <img src="/images/admin/logo.jpg" />

        <form class="form-horizontal" data-bind="submit: submit">
            <fieldset>
                <legend>Авторизация</legend>
                <div class="control-group">
                    <div class="alert" data-bind="text: message, css: { 'alert-error': hasError, 'alert-info': !hasError() }"></div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="username">Имя пользователя</label>
                    <div class="controls">
                        <input id="username" type="text" data-bind="value: login" />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="password">Пароль</label>
                    <div class="controls">
                        <input id="password" type="password" data-bind="value: password" />
                    </div>
                </div>
                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">Войти</button>
                </div>
            </fieldset>
        </form>
    </div>
</div>

<script type="text/javascript">
    function LoginViewModel() {
        var self = this

        self.message = ko.observable('Пожалуйста, введите имя пользователя и пароль')
        self.login = ko.observable()
        self.password = ko.observable()
        self.hasError = ko.observable(false)

        self.submit = function() {
            $.ajax({
                url: '/admin/login/check',
                data: {login: self.login(), password: self.password()},
                success: function(data) {
                    if (data.error) {
                        self.hasError(true)
                        self.message(data.error)
                        $("#username").focus()
                        self.password('')
                    } else if (data == 'ok') {
                        location.reload()
                    }
                }
            })
        }
    }
    ko.applyBindings(new LoginViewModel())
</script>
{% endblock %}